package com.bing.util;

import com.bing.beans.Person;
import com.bing.beans.WeiBo;
import com.bing.sina.SinaJdbc;
import com.bing.sina.TaoListener;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * Created by kaibb on 14-8-19.
 */


public class WeiBoParse implements Runnable {
    private Logger logger = LoggerFactory.getLogger(WeiBoParse.class);
    private String html;

    private Person person;

    private SinaJdbc sinaJdbc;

    private TaoListener taoListener;

    private ExcelUtil excelUtil;

    public WeiBoParse(String html, Person person, SinaJdbc sinaJdbc, TaoListener taoListener, ExcelUtil excelUtil) {
        this.html = html;
        this.person = person;
        this.sinaJdbc = sinaJdbc;
        this.taoListener = taoListener;
        this.excelUtil = excelUtil;
    }


    @Override
    public void run() {
        Document doc = Jsoup.parse(html);
        Elements weiBos = doc.select("div.W_main_c div.PRF_modwrap div.WB_feed_datail");
        logger.info("获取微波数：{}", weiBos.size());
        for (Element weiBo : weiBos) {
            String text = weiBo.text();
            WeiBo wb = TaoMath.toWeiBo(text);
            wb.setPerson(person);
            sinaJdbc.saveWeiBo(wb);
            synchronized (this) {
                excelUtil.writeWeiBo(wb);
            }
            Statistics.weiBo.incrementAndGet();
            Statistics.tongji(taoListener);
        }
    }
}
